<?php

namespace App\Models\Dev;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\Common\CommonModel;
class DevInfo extends CommonModel
{
    use HasFactory;

    protected $table = 'dev_info';
    protected $appends = ['status_name'];

    public function getLog(){
        return $this->hasMany(DevLog::class,'dev_id','id');
    }

    public function getType(){
        return $this->hasOne(DevType::class,'id','type_id');
    }

    public function getStatusNameAttribute(){
        return $this->status==1?'启用':'禁用';
    }

    public function getLst(array $conditions = []){
        $records = parent::getLst($conditions); // TODO: Change the autogenerated stub
        if(!empty($conditions['type_id'])){
            $records = $records->where('type_id',$conditions['type_id']);
        }
        if(!empty($conditions['code'])){
            $records = $records->where('code',$conditions['code']);
        }
        if(!empty($conditions['area'])){
            $records = $records->where('area','like',implode(',',$conditions['area']).'%');
        }
        if(!empty($conditions['business_type'])){
            $records = $records->whereHas('getType',function ($q) use ($conditions){
                $q->where('business_type',$conditions['business_type']);
            });
        }

        return $records;
    }
}
